namespace SinaFinance.SummaryInfoLibrary
{
    using SinaFinance;
    using SinaFinanceBaseClassLibrary;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    public partial class SearchContrast : UserControl
    {
        //private bool _contentLoaded;
        //internal Button btnSearch;
        private DB_SinaDataHelp Dsdh;
        //internal ListBox lstPopSuggest;
        private ListBoxItem nowItem = new ListBoxItem();
        private PopUpCookieDelegate popUpCookieSymbol;
        //internal Canvas SearchCanvas;
        private bool txtChangeByItem = false;
        //internal TextBox txtSearch;

        public event PopUpCookieDelegate PopUpCookieSymbol
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.popUpCookieSymbol = (PopUpCookieDelegate)Delegate.Combine((Delegate)this.popUpCookieSymbol, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.popUpCookieSymbol = (PopUpCookieDelegate)Delegate.Remove((Delegate)this.popUpCookieSymbol, (Delegate)value);
            }
        }

        public SearchContrast()
        {
            this.InitializeComponent();
        }

        private void AddItemtoHistoryView(string item)
        {
            string cookie = CookieControl.GetCookie("cookie_history");
            List<string> list = null;
            if (!string.IsNullOrEmpty(cookie))
            {
                list = cookie.Split(new char[] { '$' }).ToList<string>();
                if (!list.Contains(item))
                {
                    if (list.Count == 6)
                    {
                        CookieControl.SetCookie("cookie_history", cookie.Substring((int) (cookie.IndexOf('$') + 1)) + item + "$");
                    }
                    else
                    {
                        CookieControl.SetCookie("cookie_history", cookie + item + "$");
                    }
                }
            }
            else
            {
                CookieControl.SetCookie("cookie_history", cookie + item + "$");
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (this.lstPopSuggest.Items.Count != 0)
            {
                if (this.popUpCookieSymbol != null)
                {
                    if (this.lstPopSuggest.Items.Count == 1)
                    {
                        this.popUpCookieSymbol(((ListBoxItem)this.lstPopSuggest.Items[0]).Content.ToString().Split(new char[] { ' ' })[0], ((ListBoxItem)this.lstPopSuggest.Items[0]).Content.ToString().Split(new char[] { ' ' })[3]);
                        this.AddItemtoHistoryView(((ListBoxItem) this.lstPopSuggest.Items[0]).Content.ToString().Split(new char[] { ' ' })[0] + "_" + ((ListBoxItem) this.lstPopSuggest.Items[0]).Content.ToString().Split(new char[] { ' ' })[3] + "_" + ((ListBoxItem) this.lstPopSuggest.Items[0]).Content.ToString().Split(new char[] { ' ' })[1]);
                    }
                    else
                    {
                        this.popUpCookieSymbol(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0], this.nowItem.Content.ToString().Split(new char[] { ' ' })[3]);
                        this.AddItemtoHistoryView(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[3] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[1]);
                    }
                }
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
            }
        }

        private void Dsdh_GetSuggestDataComplated(List<DB_SUGGEST> _DB_Suggest)
        {
            if (_DB_Suggest == null)
            {
                this.lstPopSuggest.Items.Clear();
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
                this.btnSearch.IsEnabled = false;
            }
            else
            {
                this.lstPopSuggest.Visibility = Visibility.Visible;
                this.lstPopSuggest.Items.Clear();
                if (_DB_Suggest.Count == 1)
                {
                    this.btnSearch.IsEnabled = true;
                }
                else
                {
                    this.btnSearch.IsEnabled = false;
                }
                this.lstPopSuggest.Margin = new Thickness(0.0, (double) (-(_DB_Suggest.Count * 0x16) - 2), 0.0, 0.0);
                for (int i = 0; i < _DB_Suggest.Count; i++)
                {
                    ListBoxItem item = new ListBoxItem {
                        Content = _DB_Suggest[i].Symbol6 + " " + _DB_Suggest[i].Name + " " + _DB_Suggest[i].PinYin + " " + _DB_Suggest[i].Symbol8
                    };
                    this.lstPopSuggest.Items.Add(item);
                }
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/SummaryInfoLibrary/SearchContrast.xaml", UriKind.Relative));
        //        this.SearchCanvas = (Canvas) base.FindName("SearchCanvas");
        //        this.txtSearch = (TextBox) base.FindName("txtSearch");
        //        this.btnSearch = (Button) base.FindName("btnSearch");
        //        this.lstPopSuggest = (ListBox) base.FindName("lstPopSuggest");
        //    }
        //}

        private void lstPopSuggest_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Key == Key.Up) && (this.lstPopSuggest.SelectedIndex == 0))
            {
                this.txtSearch.Focus();
                this.txtSearch.SelectionStart = this.txtSearch.Text.Length;
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
            }
            else if (((e.Key == Key.Enter) && (this.lstPopSuggest.SelectedIndex != -1)) && (this.popUpCookieSymbol != null))
            {
                this.popUpCookieSymbol(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0], this.nowItem.Content.ToString().Split(new char[] { ' ' })[3]);
                this.AddItemtoHistoryView(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[3] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[1]);
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
            }
        }

        private void lstPopSuggest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((this.lstPopSuggest.SelectedIndex != -1) && (this.popUpCookieSymbol != null))
            {
                this.popUpCookieSymbol(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0], this.nowItem.Content.ToString().Split(new char[] { ' ' })[3]);
                this.AddItemtoHistoryView(this.nowItem.Content.ToString().Split(new char[] { ' ' })[0] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[3] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[1]);
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
            }
        }

        private void lstPopSuggest_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((this.lstPopSuggest.Items.Count != 0) && (this.lstPopSuggest.SelectedItem != null))
            {
                this.nowItem = (ListBoxItem) this.lstPopSuggest.SelectedItem;
                this.txtSearch.Text = this.nowItem.Content.ToString().Split(new char[] { ' ' })[0] + " " + this.nowItem.Content.ToString().Split(new char[] { ' ' })[1];
                this.txtSearch.DataContext = this.nowItem.Content.ToString().Split(new char[] { ' ' })[3] + "_" + this.nowItem.Content.ToString().Split(new char[] { ' ' })[1];
                this.btnSearch.IsEnabled = true;
                this.txtChangeByItem = true;
            }
        }

        public void OutsideListLoseFocus()
        {
            if (this.lstPopSuggest.Visibility == Visibility.Visible)
            {
                this.lstPopSuggest.Items.Clear();
                this.lstPopSuggest.Visibility = Visibility.Collapsed;
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if ((!string.IsNullOrEmpty(((TextBox) sender).Text) && (e.Key == Key.Down)) && (this.lstPopSuggest.Items.Count != 0))
            {
                this.lstPopSuggest.Focus();
                this.lstPopSuggest.SelectedIndex = 0;
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!this.txtChangeByItem)
            {
                if (!string.IsNullOrEmpty(((TextBox) sender).Text))
                {
                    if (this.Dsdh == null)
                    {
                        this.Dsdh = new DB_SinaDataHelp();
                        this.Dsdh.GetSuggestDataComplated += new GetSuggestDataHelp(this.Dsdh_GetSuggestDataComplated);
                    }
                    this.Dsdh.GetSuggestData(((TextBox) sender).Text);
                }
                else
                {
                    this.lstPopSuggest.Items.Clear();
                    this.lstPopSuggest.Visibility = Visibility.Collapsed;
                    this.btnSearch.IsEnabled = false;
                }
            }
            else
            {
                this.txtChangeByItem = false;
            }
        }

        public delegate void PopUpCookieDelegate(string Symbol6, string Symbol8);
    }
}
